package com.yungu.swift.admin.route.finance;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.utils.ExcelUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.order.service.WxToBankService;
import com.yungu.swift.system.finance.model.param.AdminAccountFlowPageParam;
import com.yungu.swift.system.finance.model.param.AdminAccountPageParam;
import com.yungu.swift.system.sys.model.vo.SysDriverAccountFlowVo;
import com.yungu.swift.system.sys.model.vo.SysDriverAccountVo;
import com.yungu.swift.system.sys.service.SysAccountDriverService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.admin.consts.AdminErrorEnum.PARAM_ERROR;

/**
 * @author : cuixiuyin
 * @date : 2019/8/26
 */
@RestController
@RequestMapping("/admin/v1/finance/balance/driver")
@Api(value = "【财务管理】", tags = "司机账户管理")
@Slf4j
public class DriverBalanceAdmin {

    @Reference
    private SysAccountDriverService sysAccountDriverService;
    @Reference
    private WxToBankService wxToBankService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "司机账户分页查询", httpMethod = "POST")
    public ResponseData<PageVo<SysDriverAccountVo>> queryPage(@RequestBody AdminAccountPageParam accountPageParam) {
        log.info("请求参数{}", MapUtils.convertObjToMap(accountPageParam));
        return sysAccountDriverService.queryPageVo(accountPageParam);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "司机账户分页查询导出", httpMethod = "POST")
    public void export(@RequestBody AdminAccountPageParam accountPageParam, HttpServletResponse response) {
        List<SysDriverAccountVo> list = sysAccountDriverService.export(accountPageParam).getData();
        Map<Object, Object>[] dictionaryMapArray = new Map[6];
        ExcelUtil.export(response, "司机余额", list, 6,
                new String[]{"司机信息", "代理/企业", "账户总金额", "可提现余额（元）", "冻结余额（元）", "已提现金额（元）"},
                new String[]{"#accountName#(#mobile#)", "#agentName#/#companyName#", "balanceTotal", "canFrowardBalance", "frozenBalance", "frowardAccumulate"},
                dictionaryMapArray);
    }

    @RequestMapping(value = "/statistics", method = RequestMethod.POST)
    @ApiOperation(value = "司机账户统计", httpMethod = "POST")
    public ResponseData<SysDriverAccountVo> statistics(@RequestBody AdminAccountPageParam accountPageParam) {
        return sysAccountDriverService.statistics(accountPageParam);
    }


    @RequestMapping(value = "/flowQueryPage", method = RequestMethod.POST)
    @ApiOperation(value = "司机账户流水分页查询", httpMethod = "POST")
    public ResponseData<PageVo<SysDriverAccountFlowVo>> flowQueryPage(@RequestBody AdminAccountFlowPageParam flowPageParam) {
        if (ParamUtil.isIllegal(flowPageParam.getUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return sysAccountDriverService.flowQueryPage(flowPageParam);
    }

    @RequestMapping("/flowExport")
    @ApiOperation(value = "司机账户流水导出", httpMethod = "POST")
    public void flowExport(@RequestBody AdminAccountFlowPageParam flowPageParam, HttpServletResponse response) {
        if (ParamUtil.isIllegal(flowPageParam.getUuid())) {
            return;
        }
        ResponseData<List<SysDriverAccountFlowVo>> flowExport = sysAccountDriverService.flowExport(flowPageParam);
        if (flowExport.isSuccess() && CollectionUtils.isNotEmpty(flowExport.getData())) {
            List<SysDriverAccountFlowVo> flowExportData = flowExport.getData();
            Map<Object, Object>[] dictionaryMapArray = new Map[6];
            ExcelUtil.export(response, "司机账户流水", flowExportData, 6,
                    new String[]{"时间", "流水号", "订单号", "交易类型", "交易类目", "余额（元）"},
                    new String[]{"createOn", "financeNumber", "orderNo", "transactionType", "courseDescription", "amount"},
                    dictionaryMapArray);
        }
    }

}
